home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / xinetd.2 / xinetd / xinetd.2.1.7-linux.4 / libs / src / misc / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-10  |  3.5 KB  |  203 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: misc.c,v 1.6 1995/09/10 18:32:56 chuck Exp $" ;
  8. static char misc_version[] = VERSION ;
  9.  
  10. #include <varargs.h>
  11. #include <sys/param.h>
  12. #if defined(linux) || defined(BSD)
  13. #include <stdlib.h>
  14. #endif
  15.  
  16. /*
  17.  * MISCELLANEOUS FUNCTIONS
  18.  */
  19.  
  20. #include "misc.h"
  21.  
  22. #ifndef NULL
  23. #define NULL        0
  24. #endif
  25.  
  26. char *strncpy() ;
  27. char *strrchr() ;
  28.  
  29.  
  30. /*
  31.  * Create a new argv array,
  32.  * copy the original to the new one,
  33.  * and clear the old one
  34.  */
  35. char **argv_copy_and_clear( org_argv, start, count )
  36.     char **org_argv ;                            /* original argv */
  37.     int start ;                                    /* from where to start copy/clear */
  38.     int count ;                                    /* how many entries to copy/clear */
  39. {
  40.     char **new_argv ;
  41.     char *p ;
  42.     int i ;
  43.     int j ;
  44. #if !defined(linux) && !defined(BSD)
  45.     char *malloc() ;
  46. #endif
  47.  
  48.     new_argv = (char **) malloc( count * sizeof( char * ) ) ;
  49.     if ( new_argv == NULL )
  50.         return( NULL ) ;
  51.  
  52.     for ( i = 0 ; i < count ; i++ )
  53.     {
  54.         new_argv[ i ] = make_string( 1, org_argv[ start+i ] ) ;
  55.         if ( new_argv[ i ] == NULL )
  56.         {
  57.             for ( j = i-1 ; j >= 0 ; j-- )
  58.                 free( new_argv[ j ] ) ;
  59.             free( (char *) new_argv ) ;
  60.             return( NULL ) ;
  61.         }
  62.         for ( p = org_argv[ start+i ] ; *p ; p++ )
  63.             *p = ' ' ;
  64.     }
  65.     return( new_argv ) ;
  66. }
  67.  
  68.  
  69. /*
  70.  * We always return a pointer in pathname
  71.  */
  72. char *basename( pathname )
  73.     char *pathname ;
  74. {
  75.     char *s = strrchr( pathname, '/' ) ;
  76.  
  77.     if ( s == NULL )
  78.         return( pathname ) ;
  79.     else
  80.         return( s+1 ) ;
  81. }
  82.  
  83.  
  84. /*
  85.  * We always return a malloced string
  86.  *
  87.  * There are 2 special cases:
  88.  *
  89.  *        1) pathname == "/"
  90.  *                In this case we return "/"
  91.  *        2) pathname does not contain a '/'
  92.  *                In this case we return "."
  93.  */
  94. char *dirname( pathname )
  95.     char *pathname ;
  96. {
  97.     int len ;
  98.     char *s = strrchr( pathname, '/' ) ;
  99.     char *p ;
  100. #if !defined(linux) && !defined(BSD)
  101.     char *malloc() ;
  102. #endif
  103.  
  104.     if ( s == NULL )
  105.         return( make_string( 1, "." ) ) ;
  106.     else
  107.     {
  108.         len = s - pathname ;
  109.         if ( len == 0 )
  110.             return( make_string( 1, "/" ) ) ;
  111.     }
  112.  
  113.     p = malloc( len+1 ) ;
  114.     if ( p == NULL )
  115.         return( NULL ) ;
  116.     else
  117.     {
  118.         strncpy( p, pathname, len )[ len ] = '\0' ;
  119.         return( p ) ;
  120.     }
  121. }
  122.  
  123.  
  124. char *make_string( count, va_alist )
  125.     register unsigned count ;
  126.     va_dcl
  127. {
  128.     va_list ap ;
  129.     register unsigned i ;
  130.     register unsigned len = 0 ;
  131.     register char *s, *p ;
  132.     char *new_string ;
  133.  
  134.     if ( count == 0 )
  135.         return( NULL ) ;
  136.  
  137.     va_start( ap ) ;
  138.     for ( i = 0 ; i < count ; i++ )
  139.     {
  140.         s = va_arg( ap, char * ) ;
  141.         if ( s == NULL )
  142.             continue ;
  143.         len += strlen( s ) ;
  144.     }
  145.     va_end( ap ) ;
  146.  
  147.     new_string = malloc( len + 1 ) ;
  148.     if ( new_string == NULL )
  149.         return( NULL ) ;
  150.  
  151.     p = new_string ;
  152.     va_start( ap ) ;
  153.     for ( i = 0 ; i < count ; i++ )
  154.     {
  155.         s = va_arg( ap, char * ) ;
  156.         if ( s == NULL )
  157.             continue ;
  158.         while ( *p++ = *s++ ) ;
  159.         p-- ;
  160.     }
  161.     va_end( ap ) ;
  162.     return( new_string ) ;
  163. }
  164.  
  165.  
  166. char *make_pathname( count, va_alist )
  167.     register unsigned count ;
  168.     va_dcl
  169. {
  170.     va_list ap ;
  171.     register unsigned i ;
  172.     register unsigned len = 0 ;
  173.     register char *s, *p ;
  174.     char *pathname ;
  175.  
  176.     if ( count == 0 )
  177.         return( NULL ) ;
  178.  
  179.     va_start( ap ) ;
  180.     for ( i = 0 ; i < count ; i++ )
  181.     {
  182.         s = va_arg( ap, char * ) ;
  183.         len += strlen( s ) ;
  184.     }
  185.     va_end( ap ) ;
  186.  
  187.     pathname = malloc( len + count ) ;
  188.     if ( pathname == NULL )
  189.         return( NULL ) ;
  190.     
  191.     p = pathname ;
  192.     va_start( ap ) ;
  193.     for ( i = 0 ; i < count ; i++ )
  194.     {
  195.         s = va_arg( ap, char * ) ;
  196.         while ( *p++ = *s++ ) ;
  197.         *(p-1) = '/' ;            /* change '\0' to '/' */
  198.     }
  199.     *(p-1) = '\0' ;
  200.     return( pathname ) ;
  201. }
  202.  
  203.